﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyThietBi
{
    public partial class frmquanlyloaivattu : Form
    {
        CXuly_loaivattu xuly;
        SqlConnection con;
        public frmquanlyloaivattu(SqlConnection con)
        {
            InitializeComponent();
            this.con = con;
            xuly = new CXuly_loaivattu(con);
        }
        private void frmquanlyloaivattu_Load(object sender, EventArgs e)
        {
            hienthi();
        }
        public void hienthi()
        {
            List<CLoaivattu> list = xuly.getdsloaivattu();
            lstLoaivattu.Items.Clear();
            foreach (CLoaivattu vt in list)
            {
                ListViewItem lvi = lstLoaivattu.Items.Add(vt.Maloai);
                lvi.SubItems.Add(vt.Tenloai);
            }
        }
        private void lstLoaivattu_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lstLoaivattu.SelectedItems)
            {
                string maloai = lvi.SubItems[0].Text;
                CLoaivattu data = xuly.tim(maloai);
                if (data != null)
                {
                    txtmaloai.Text = data.Maloai;
                    txttenloai.Text = data.Tenloai;

                }
                break;
            }
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            CLoaivattu a = new CLoaivattu();
            a.Maloai = txtmaloai.Text.ToString();
            a.Tenloai = txttenloai.Text.ToString();
            if(!xuly.them(a))
                MessageBox.Show("Trùng khoá chính");
            hienthi();
        }
        private void btnxoa_Click(object sender, EventArgs e)
        {
            string maloai = txtmaloai.Text;
            xuly.xoa(maloai);
            hienthi();
        }
        private void btnsua_Click(object sender, EventArgs e)
        {
            CLoaivattu a = new CLoaivattu();
            a.Maloai = txtmaloai.Text.ToString();
            a.Tenloai = txttenloai.Text.ToString();
            xuly.sua(a);
            hienthi();
        }

        
    }
}
